package project.teg.views;

import project.teg.R;
import project.teg.configuration.Gestures;
import project.teg.views.ColorPickerDialog.OnColorChangedListener;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

public class IconPreference extends Preference implements
		OnColorChangedListener {

	public int circleColor = Color.BLACK;
	public String preferenceName;
	private View view;

	public IconPreference(Context context, AttributeSet attrs) {
		super(context, attrs);
		setLayoutResource(R.layout.preference_icon);
	}

	@Override
	public void onBindView(View view) {
		super.onBindView(view);
		drawCircleColor(view);
	}

	public void refresh() {
		drawCircleColor(view);

	}

	private void drawCircleColor(View view) {
		this.view = view;
		ImageView imageView = (ImageView) view.findViewById(R.id.icon);
		if (imageView != null) {
			Bitmap b = Bitmap.createBitmap(50, 50, Config.ARGB_4444);
			Canvas c = new Canvas(b);
			Paint pathPaint = new Paint();
			pathPaint.setColor(circleColor);
			pathPaint.setStyle(Paint.Style.FILL);
			pathPaint.setAntiAlias(true);
			c.drawOval(new RectF(5, 5, 45, 45), pathPaint);
			imageView.setImageBitmap(b);
		}
	}

	@Override
	public void colorChanged(int color) {
		Gestures.saveColor(preferenceName, color, this.getContext());
		circleColor = color;
		refresh();		
	}
}
